package com.ambow.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import com.ambow.Entity.Operator;
import com.ambow.dao.Operat;



/**
 * Servlet implementation class Person_userServlet
 */
@WebServlet("/Person_userServlet")
public class Person_userServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Person_userServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.setHeader("content-type", "text/html;charset=UTF-8");
		String ud=request.getParameter("update");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");  
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession  hs = request.getSession(); 
		Operat operat = new Operat();
		
		if(ud.equals("user_modify")){
//			String position=new String(request.getParameter("textfield3").getBytes("ISO-8859-1"),"utf-8");
			String position = request.getParameter("textfield3");
			String telephone = request.getParameter("textfield8");

			String remark = request.getParameter("textarea");
			if(remark==null){
				
				remark=(String)hs.getAttribute("operator_remark");
			}

			System.out.println("新职位："+position);
			System.out.println("新电话："+telephone);
			System.out.println("新备注："+remark);
			
			int operator_id=(Integer) hs.getAttribute("operator_id");


			
			boolean f=operat.updateinfo(operator_id, position, remark, telephone);
			if(f==true){
				Operator op=operat.getOne(Operator.class, operator_id);
				HttpSession session=request.getSession();
				session.setAttribute("operator_id", op.getId());
				session.setAttribute("operator_name", op.getName());
				session.setAttribute("operator_posetion", op.getPosition());
				//String op_postion;
				//request.setAttribute("opPostion", op.getPosetion());
				session.setAttribute("operator_tel", op.getTel());
				//request.setAttribute("opTel", op.getTel());
				session.setAttribute("operator_role", op.getRole());
				//request.setAttribute("opRole", op.getRole());
				session.setAttribute("operator_remark", op.getRemark());
				//request.setAttribute("opRemark", op.getRemark());
				session.setAttribute("operator_password", op.getPassword());
				request.getRequestDispatcher("pages/module/person/person001/user_detail.jsp").forward(request, response);
			}
			
		}
		else if(ud.equals("user_add")){
			
			String new_account = request.getParameter("textfield");
			
			String new_name = request.getParameter("textfield2");
//			byte [] b=new_name.getBytes("iso-8859-1");
//			new_name=new String(b,"utf-8");
			
			String new_psw = request.getParameter("textfield7");
			
			String confirm_psw = request.getParameter("ctextfield7");
			
			String new_position = request.getParameter("textfield3");
//			byte [] b1=new_postion.getBytes("iso-8859-1");
//			new_postion=new String(b1,"utf-8");
			
			String new_tel = request.getParameter("textfield8");
			
			String new_role = request.getParameter("select11");
//			byte [] b2=new_role.getBytes("iso-8859-1");
//			new_role=new String(b2,"utf-8");
			
			String new_remark = request.getParameter("textarea");
//			byte [] b3=new_remark.getBytes("iso-8859-1");
//			new_remark=new String(b3,"utf-8");
			
			System.out.println(new_account);
			System.out.println(new_name);
			System.out.println(new_psw);
			System.out.println(confirm_psw);
			System.out.println(new_position);
			System.out.println(new_tel);
			System.out.println(new_role);
			System.out.println(new_remark);
			
			boolean f = operat.addUser(new_account, new_name, new_psw, new_position, new_tel, new_role, new_remark);
			
			if(f==true){
				request.getRequestDispatcher("pages/module/system/system002/user_list.jsp").forward(request, response);
			}
			
		}
		else if(ud.equals("role_query")){
			String role_query = request.getParameter("group1");
//			byte[] b=role_query.getBytes("iso-8859-1");
//			role_query=new String(b,"utf-8");
			System.out.println(role_query);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
